class RightsController < ApplicationController
  before_filter :user_authorize
  layout "index",:except => [:getRights,:addRight,:editRight,:delRight,:getRightByID,:getRightsByUserID]
  def rightList
    rights = session[:user_rights]
    rightPromise = RightUtil.filterRight(rights,params[:controller],params[:action])
    if rightPromise == false
      redirect_to(:controller => "index", :action => "index") and return
    end
  end
  
  def getRights
    begin
      pageBegin =  params[:start]
      pageEnd =  params[:limit]
      strdelData = params[:delData]
      if strdelData!=nil
        delRight(strdelData)
        
      end
      strsaveData = params[:saveData]
      userID = params[:user_id]
      if strsaveData!=nil
        saveRightForUser(strsaveData,userID)
      end
      pageRights = Right.find(:all,:conditions=>["is_locked <> 1"])
      if pageBegin ==nil
        rights = Right.find(:all,:conditions=>["is_locked <> 1"],:select=>"id,controller_name,action_name,description")
      else
        rights = Right.find(:all,:conditions=>["is_locked <> 1"],:select=>"id,controller_name,action_name,description",:limit =>pageBegin+","+pageEnd)
      end
      jsonstr="{totalProperty:"+pageRights.length.to_s+",root:"+rights.to_json()+"}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Add User
  def addRight
    begin
      right = Right.new
      right.controller_name=params[:controller_name]
      right.action_name=params[:action_name]
      right.description=params[:description]
      right.save
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Delete User
  def delRight(strdelData)
    ids=""
    begin
      results = ActiveSupport::JSON.decode(strdelData)
      results.each do |result|
        ids << result["id"]+","
      end
      ids = ids[0,ids.length-1]
      Right.update_all("is_locked = 1","id in ("+ids+")")
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  #
  def saveRightForUser(strsaveData,userID)
    #ids=""
    begin
      UsersRight.delete_all(["user_id = ?",userID])
      if strsaveData!="0"
        results = ActiveSupport::JSON.decode(strsaveData)
        results.each do |result|
          usersRight =  UsersRight.new
          usersRight.user_id=userID
          usersRight.right_id=result["id"]
          usersRight.save
        end
      end      
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  # Get User By ID
  def getRightByID
    begin
      right = Right.find(params[:id],:select=>"id, controller_name, action_name, description")
      jsonstr="{root:["+right.to_json()+"]}"
      render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
      
    end
  end
  
  #Edit User
  def editRight
    begin
      right = Right.find(params[:id])
      right.controller_name=params[:controller_name]
      right.action_name=params[:action_name]
      right.description=params[:description]
      right.save
      #jsonstr="{root:["+user.to_json()+"]}"
      #render :text=>jsonstr, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
  
  
  def getRightsByUserID
    begin
      user_id =  params[:user_id]
      user_rights=User.find_by_id(user_id).rights
      rightids=""
      user_rights.each do |user_right|
        rightids=rightids+user_right.id.to_s+","
      end
      rightids=rightids[0,rightids.length-1]
      #jsonstr="{success:true,totalProperty:"+user_rights.length.to_s+",root:"+rightids.to_json()+"}"
      render :text=>rightids, :layout=>false
    rescue Exception => e
      CommonHelper.infoLogger(session[:user_id],request.env["REMOTE_ADDR"],params[:controller],params[:action],e,"error")
    end
  end
end
